<template>
  <div>
      <el-card class="box-card">
      <span><i class="iconfont icon-dingdan1 icon"> </i>产品分类数据列表</span>
      <el-button
        style="float: right; padding: 3px"
        type="primary"
        @click="goCateAddPage"
        >添加</el-button
      >
       <el-button
        style="float: right; padding: 3px; margin-right: 5px"
        type="primary"
        v-if="parentId != 0"
        @click="goBackLevel"
        >返回上级节点</el-button
      >
    </el-card>
       <div class="product_con">
      商品列表
     <el-table :data="cateList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="分类名称 "> </el-table-column>
      <el-table-column  label="级别 "> 
         <template slot-scope="scope">
          {{ scope.row.level | jibeiFilter }}
        </template>
      </el-table-column>   
      <el-table-column   prop="product_count" label="商品数量 "> </el-table-column>
      <el-table-column   prop="product_unit" label="数量单位 "> </el-table-column>
      <el-table-column   label="是否显示 " align="center"> 
       <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show_status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column   prop="level" label="排序 "> </el-table-column>
      <el-table-column    label="设置 ">
      <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="showNextLevel(scope.row.id)"
            :disabled="scope.row.level == 1"
            >查看下级</el-button
          >
        </template>
      </el-table-column>
       <el-table-column  label="操作" > 
           <div class="click">
            <el-button class="click1" @click="editFun(scope.row.id)"> 编辑</el-button> 
             <el-button type="danger" class="click2" @click="delFun(scope.row.id)">删除</el-button>
           </div>
   </el-table-column>
    </el-table>
   </div>
    
   
      
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[2,5, 10, 15]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
     


  </div>
</template>

<script>
//引入
import { fetchProductCatePage } from "@/api/product";
export default {
  name: "productCate",
  data: function () {
    return {
      cateList: [],
      //1.分页的数据
        listQuery:{
        pageSize:5,//一页10条数据
        pageNum:1,//当前页
      },
      total:0,  //数据的总数
      parentId:0, //父级id号 查看下一级和上一级
    };
  },
  filters:{
     jibeiFilter(val) {
      return val ? "二级" : "一级";
    },
  },
  methods: {
    //获取数据
    async getCateList() {
     const res = await fetchProductCatePage(this.listQuery, this.parentId);
      console.log(res);
      this.cateList = res.data;
       //3..获取数据的总量
      this.total=res.total
    },
        //修改
    editFun(id) {
      console.log("修改触发了", id);
    },
    //删除
    delFun(id) {
      console.log("删除触发了", id);
    },
    //查看下一级
    showNextLevel(id){
     this.parentId = id;
      this.getCateList();
    },
    //返回上一级
 goBackLevel() {
      this.parentId = 0;
      this.getCateList();
    },
     // 修改每页显示的条数
    handleSizeChange(val) {
      console.log("修改每页显示的条数 触发了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      //重新获取数据
      this.getCateList();
    },
    //6.修改当前页的页码
    handleCurrentChange(val) {
      console.log("修改当前页的页码 触发了", val);
      this.listQuery.pageNum = val;
       this.getCateList();
    },
    //跳转页面
     goCateAddPage() {
      this.$router.push("/pms/productCateAdd");
    },
  },
  mounted() {
    this.getCateList();
  },
};
</script>

<style lang="scss">
 .ding{
   width: 99%;
   height:50px ;
   border:1px solid rgb(161, 158, 158);
   margin-left: 10px;
   margin-top: 10px;
   border-radius: 5px;
   display: flex;
   justify-content: space-between;
   div{
      width: 70px;
     height: 24px;
   }
   .click{
     width: 46px;
     height: 24px;
   }
 }
 .product_con{
    margin-left: 10px;
     width: 99%;
 }
 .click{
   width: 400px;
 }
</style>